package number_2;

import java.util.List;

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode curr = dummy;
        int add = 0;
        while (l1 != null || l2 != null || add != 0) {
            int v1 = l1 == null ? 0 : l1.val;
            int v2 = l2 == null ? 0 : l2.val;
            int value = v1 + v2 + add;
            curr.next = new ListNode(value % 10);
            curr = curr.next;
            add = value / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        return dummy.next;
    }

    static class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }

    public static void main(String[] args) {
        ListNode node = new ListNode(2);
        ListNode node1 = new ListNode(4);
        node.next = node1;
        node1.next = new ListNode(3);

        ListNode root = new ListNode(5);
        ListNode root1 = new ListNode(6);
        root.next = root1;
        root1.next = new ListNode(4);

        new Solution().addTwoNumbers(node, root);

    }
}
